package com.sgcc.nfc.lock.presentation.warnings

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sgcc.nfc.lock.core.network.NetworkResult
import com.sgcc.nfc.lock.domain.model.Warning
import com.sgcc.nfc.lock.domain.usecase.GetWarningDetailUseCase
import com.sgcc.nfc.lock.domain.usecase.HandleWarningUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.launch

@HiltViewModel
class WarningDetailViewModel @Inject constructor(
    private val getWarningDetailUseCase: GetWarningDetailUseCase,
    private val handleWarningUseCase: HandleWarningUseCase,
    savedStateHandle: SavedStateHandle
) : ViewModel() {

    private val warningId: Long = savedStateHandle.get<Long>("warningId") ?: 0L

    private val _warning = MutableLiveData<Warning?>()
    val warning: LiveData<Warning?> = _warning

    private val _isLoading = MutableLiveData(false)
    val isLoading: LiveData<Boolean> = _isLoading

    private val _message = MutableLiveData<String?>()
    val message: LiveData<String?> = _message

    init {
        loadDetail()
    }

    fun loadDetail() {
        viewModelScope.launch {
            _isLoading.value = true
            _message.value = null
            
            when (val result = getWarningDetailUseCase(warningId)) {
                is NetworkResult.Success -> {
                    _warning.value = result.data
                }
                is NetworkResult.Error -> {
                    _message.value = result.message
                }
                is NetworkResult.Loading -> {
                    // Loading state already set
                }
            }
            
            _isLoading.value = false
        }
    }

    fun handleWarning(remark: String?) {
        viewModelScope.launch {
            Log.d("WarningDetailVM", "开始处理预警，warningId: $warningId, remark: $remark")
            _isLoading.value = true
            _message.value = null
            
            // 验证warningId
            if (warningId <= 0L) {
                Log.e("WarningDetailVM", "预警ID无效: $warningId")
                _message.value = "预警ID无效，无法处理预警"
                _isLoading.value = false
                return@launch
            }
            
            when (val result = handleWarningUseCase(warningId, remark)) {
                is NetworkResult.Success -> {
                    Log.d("WarningDetailVM", "预警处理成功")
                    _message.value = "预警处理成功"
                    // 重新加载详情
                    loadDetail()
                }
                is NetworkResult.Error -> {
                    Log.e("WarningDetailVM", "预警处理失败: ${result.message}, code: ${result.code}")
                    _message.value = result.message ?: "处理预警时发生未知错误"
                }
                is NetworkResult.Loading -> {
                    Log.d("WarningDetailVM", "预警处理中...")
                    // Loading state already set
                }
            }
            
            _isLoading.value = false
        }
    }
}
